package com.homedo.odin.solution.controller.mng;

import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.contract.mng.ColumnTagPageRequestBean;
import com.homedo.odin.solution.enumcode.basic.DeleteEnum;
import com.homedo.odin.solution.model.entity.ColumnTag;
import com.homedo.odin.solution.model.request.mng.tag.ColumnTagPageResponseBean;
import com.homedo.odin.solution.model.request.mng.tag.ColumnTagRequestBean;
import com.homedo.odin.solution.service.ColumnTagServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.validation.Valid;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author: ZhangLei
 * @date: 2018/4/9
 * @Description:
 */
@Api(description = "栏目标签", tags = {"ColumnTag"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/mng/tags")
public class MngColumnTagController {

    @Autowired
    private ColumnTagServiceImpl columnTagService;

    @PostMapping
    @ApiOperation(value = "OMS_新增栏目标签", httpMethod = "POST", notes = "新增不要传tagId,修改的时候才需要")
    public ResultData addColumnTag(@RequestBody ColumnTagRequestBean requestBean) {
        if (StringUtils.isBlank(requestBean.getName())) {
            throw new BizException(Meta.CODE_451.getCode(), "新增标签名不能为空");
        }
        if (StringUtils.isBlank(requestBean.getOperator())) {
            throw new BizException(Meta.CODE_451.getCode(), "操作人不能为空");
        }
        columnTagService.insertColumnTag(requestBean);
        return ResultData.newInstance(Meta.CODE_200, null);
    }

    @PutMapping
    @ApiOperation(value = "OMS_修改栏目标签", httpMethod = "PUT", notes = "修改的时候传tagId")
    public ResultData updateTag(@RequestBody ColumnTagRequestBean requestBean) {
        if (!NumberHelper.validate(requestBean.getTagId())) {
            throw new BizException(Meta.CODE_451.getCode(), "缺少tagId参数");
        }
        if (StringUtils.isBlank(requestBean.getOperator())) {
            throw new BizException(Meta.CODE_451.getCode(), "操作人不能为空");
        }
        columnTagService.updateTag(requestBean);
        return ResultData.newInstance(Meta.CODE_200, null);
    }


    @DeleteMapping("/{tagId}")
    @ApiOperation(value = "OMS_删除栏目标签", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", dataType = "String", name = "operator", value = "操作人", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "tagId", value = "栏目标签Id", required = true)})
    public ResultData deleteColumnTag(@PathVariable("tagId") Integer tagId,
            String operator) {
        if (!NumberHelper.validate(tagId)) {
            throw new BizException(Meta.CODE_451.getCode(), "缺少tagId参数");
        }
        if (StringUtils.isBlank(operator)) {
            throw new BizException(Meta.CODE_451.getCode(), "操作人不能为空");
        }

        columnTagService.deleteColumnTag(tagId, operator);
        return ResultData.newInstance(Meta.CODE_200, null);
    }


    @GetMapping("/{tagId}")
    @ApiOperation(value = "OMS_查询标签", response = ColumnTag.class, httpMethod = "GET")
    public ResultData queryTagByTagId(@ApiParam(value = "标签Id", required = true) @PathVariable("tagId") Integer tagId) {
        if (!NumberHelper.validate(tagId)) {
            throw new BizException(Meta.CODE_451.getCode(), "标签Id不合法");
        }
        ColumnTag columnTag = columnTagService.selectById(tagId);
        return ResultData.newInstance(Meta.CODE_200, columnTag);
    }


    @GetMapping("/param")
    @ApiOperation(value = "OMS_条件分页栏目标签列表", response = ColumnTag.class, httpMethod = "GET")
    public ResultData queryByParamList(@Valid @ModelAttribute ColumnTagPageRequestBean requestBean,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }

        ColumnTagPageResponseBean responseBean = columnTagService.queryByParamList(requestBean);
        return ResultData.newInstance(Meta.CODE_200, responseBean);
    }

    @GetMapping
    @ApiOperation(value = "OMS_获取所有标签列表", response = ColumnTag.class, httpMethod = "GET")
    public ResultData queryAllTag() {
        List<ColumnTag> result = columnTagService.queryAllTag(null, DeleteEnum.NOT_DEL.getDbValue());
        return ResultData.newInstance(Meta.CODE_200, result);
    }
}
